package com.woupla.company;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.jboss.solder.logging.Logger;

import com.woupla.model.Company;
import com.woupla.model.User;



/**
 * Session Bean implementation class UserEJB
 */

@Stateless
public class CompanyEJB {

	@Inject
	private Logger log;

	@PersistenceContext
	private EntityManager em;
	

	/**
	 * Default constructor.
	 */
	public CompanyEJB() {
		// TODO Auto-generated constructor stub
	}

	public List<Company> getAllCompanies() {
		@SuppressWarnings("unchecked")
		List<Company> companies = em.createNamedQuery("findAllCustomers").getResultList();
		log.info("find all companies");
		return companies;
	}

	public List<Company> findAllCompany() {
		@SuppressWarnings("unchecked")
		List<Company> companies = em.createQuery("Select c from Company c").getResultList();
		log.info("find all companies");
		return companies;
	}

	/**
	 * @param user
	 *            the user to save him
	 */
	public void saveCompany(Company company) {
		log.info("Registering " + company.getName());
		em.persist(company);
		em.refresh(company);
	}
	public void saveAdress(Company company) {
		em.merge(company.getAdress());
	}
	public void findCompany(Company companyTemp){
		Company company = (Company) em.createQuery("Select c from Company c ").getSingleResult();
		log.info(company.getName() + " " + company.getTel());
	}
	public Company findCompanyById(Long company)
	{
		TypedQuery<Company>query= em.createNamedQuery("company.findCompanyById", Company.class);
		query.setParameter("company", company);
		return query.getSingleResult();
	}
	public List<User> FindCompanyEmployees(Company company){
		TypedQuery<Company>query= em.createNamedQuery("company.findCompanyById", Company.class);
		query.setParameter("company", company.getId());
		Company result = query.getSingleResult();
		return result.getEmployees();
	}

}
